package com.zjt.web.api;

import com.zjt.service.user.IClientUserService;
import com.zjt.service.user.IReportService;
import com.zjt.web.api.vo.base.StateVO;
import com.zjt.web.api.vo.request.ReportReqVO;
import com.zjt.web.api.vo.response.ReportRespVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zjt.entity.Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-14
 * Time: AM10:51
 * 举报接口
 */

@Controller
@RequestMapping(value = "/api/report")
public class ReportAPIController {
    private static Logger logger = LoggerFactory.getLogger(ReportAPIController.class);
    private IClientUserService clientUserService;
    private IReportService reportService;
    @Autowired
    public void setClientUserService(IClientUserService clientUserService) {
        this.clientUserService = clientUserService;
    }
    @Autowired
    public void setReportService(IReportService reportService) {
        this.reportService = reportService;
    }

    /**
     * 举报
     * @param reportReqVO
     * @return
     */
    @RequestMapping(value = {"report.json", ""})
    @ResponseBody
    public ReportRespVO report(ReportReqVO reportReqVO) {
        logger.info("/api/report/report request parms = " + reportReqVO.toString());
        //TODO 进行请求参数的解析
        StateVO stateVO = new StateVO();
        try
        {
            if (this.clientUserService.checkExistByUserId(reportReqVO.getUserId()))
            {
                Report report = null;
                report = this.reportService.saveReport(reportReqVO.getUserId(),reportReqVO.getAddress(),
                        reportReqVO.getLat(),reportReqVO.getLng(),reportReqVO.getContent());
                stateVO.setCode(0);
                stateVO.setMsg("OK");
                if (report!=null)
                {
                    if (reportReqVO.getPicUrls()!=null&&reportReqVO.getPicUrls().length>0)
                    {
                        for (int i=0;i<reportReqVO.getPicUrls().length;i++)
                        {
                            String url = reportReqVO.getPicUrls()[i].toString();
                            String shortUrl = "";//图片全路径为：http：//www.zhenjiatong.com/resource/images/20120902/ac/bd/xxxx.jgp
                            //TODO 在数据库中只需要存   yyyyMMdd/ac/bd/**.jpg
                            //TODO 入库时，需要去掉前部分
                            try
                            {
                                //if (url.startsWith("http"))
                                if (url.indexOf("images")==-1)
                                    continue;
                                shortUrl = url.substring(url.lastIndexOf("images")+7,url.length()-1);
                                logger.info("shortUrl:"+shortUrl);
                                this.reportService.saveReportResource(report.getId(),shortUrl);
                            }catch (Exception e)
                            {
                                logger.error("写入举报异常"+url+":shortURL="+shortUrl);
                            }
                        }
                    }
                }

            }else
            {
                stateVO.setCode(-1);
                stateVO.setMsg("client userId 不存在");
                logger.error("client userId 不存在");
            }
        }catch (Exception e){
            stateVO.setCode(-2);
            stateVO.setMsg("举报失败");
            logger.error("举报失败,异常");
        }
        ReportRespVO reportRespVO = new ReportRespVO();
        //TODO 进行业务逻辑的处理 ， 封装应答数据
        reportRespVO.setStateVO(stateVO);
        logger.info("/api/report/report response result = " + reportRespVO.toString());
        return reportRespVO;
    }
}
